package com.lanou.moving.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.NewAlbumRoot;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 梁树珍 on 15/10/31.
 */
public class NewAlbumAdapter extends BaseAdapter{
    private Context context;
    private List<NewAlbumRoot.DataEntity> datas;

    public NewAlbumAdapter(Context context, List<NewAlbumRoot.DataEntity> datas) {
        this.context = context;
        this.datas = datas;
    }
    public void addDatas(List<NewAlbumRoot.DataEntity> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void refreshDatas(List<NewAlbumRoot.DataEntity> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.xindie_gridview_item, null);
            holder = new ViewHolder();
            holder.album_img = (ImageView) convertView.findViewById(R.id.album_img);
            holder.album_name = (TextView) convertView.findViewById(R.id.album_name);
            holder.album_singerName = (TextView) convertView.findViewById(R.id.album_singerName);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.album_name.setText(datas.get(position).getName());
        holder.album_singerName.setText(datas.get(position).getSingerName());
        Log.i("XINDIE", "--->" + datas.get(position).getSingerName());
        Picasso.with(context).load(datas.get(position).getPicUrl()).placeholder(R.mipmap.song_list_before)
                .error(R.mipmap.song_list_before).into(holder.album_img);
        return convertView;
    }
    public class ViewHolder{
        private ImageView album_img;
        private TextView album_name, album_singerName;
    }
}
